package com.hxexp.weixin.mvc.station.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.constant.CookieName;
import com.hxexp.weixin.constant.TrafficlineNC;
import com.hxexp.weixin.constant.WebAppVersion;
import com.hxexp.weixin.hpe.api.arrivestations.model.ArriveStationsParamBean;
import com.hxexp.weixin.hpe.api.arrivestations.model.ArriveStationsResultBean;
import com.hxexp.weixin.hpe.api.startstations.model.StartStationsBean;
import com.hxexp.weixin.mvc.station.service.StationService;
import com.hxexp.weixin.mvc.trafficline.model.TicketQueryParam;

/**
 * 车站控制器
 *
 * @author li.jiang
 *
 */
@Controller
@RequestMapping(value="/station")
public final class StationController {
    private static final Logger LOGGER = LoggerFactory.getLogger(StationController.class);
    private static final String TICKETQUERYPARAM_ATTR = "ticketQueryParam";
    private static final String ARRIVESTATIONS_ATTR = "arriveStations";
    @Autowired
    private StationService stationService;
    /**
     * 出发站列表页面
     * @return 出发站列表
     */
    @RequestMapping(value="/startlist")
    public String startStations(TicketQueryParam queryParam,
                                @CookieValue(value = CookieName.APP_VERSION, defaultValue = WebAppVersion.HX) String appVer, Model model) {
        // 因为重新选择出发站，所以需要清除查询参数中的到达站信息，以便用户重新选择到达站
        queryParam.setDestination("");
        queryParam.setArriveCityId("");
        queryParam.setArriveStationId("");

        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        List<StartStationsBean> startStationList = new ArrayList<StartStationsBean>();
        try {
            startStationList = stationService.getStartStations(queryParam.getGroupId()).getStartStations();
            if (WebAppVersion.XA.equalsIgnoreCase(appVer)) {
                startStationList = stationService.includeStartStations(appVer, startStationList);
            } else if (WebAppVersion.RXX.equalsIgnoreCase(appVer)) {
                startStationList = stationService.includeStartStations("RX", startStationList);
            } else if (WebAppVersion.HX.equalsIgnoreCase(appVer)) {
                startStationList = stationService.excludeStartStations(startStationList);
            }
        } catch (ServiceException e) {
            LOGGER.error("调用出发站列表服务发生异常", e);
        }
        model.addAttribute("startStations", startStationList);

        return "start_stations";
    }

    /**
     * 到达站列表页面
     * @param TicketQueryParam 车票查询请求参数
     * @return 到达站列表
     */
    @RequestMapping(value="/arrivelist")
    public String arriveStations(TicketQueryParam queryParam,
                                 @CookieValue(value = CookieName.APP_VERSION, defaultValue = WebAppVersion.HX) String appVer, Model model) {
        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        model.addAttribute(ARRIVESTATIONS_ATTR, queryArriveStations(queryParam, appVer).getArriveStations());

        return "arrive_stations";
    }
    /**
     * 到达站列表片段（根据到达站名称模糊查询时使用）
     * @param queryParam
     * @param model
     * @return
     */
    @RequestMapping(value="/ajaxArrivelist")
    public String getArriveList(TicketQueryParam queryParam,
                                @CookieValue(value = CookieName.APP_VERSION, defaultValue = WebAppVersion.HX) String appVer, Model model) {
        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        model.addAttribute(ARRIVESTATIONS_ATTR, queryArriveStations(queryParam, appVer).getArriveStations());

        return "arrive_station_fragment";
    }

    // 查询到达站
    private ArriveStationsResultBean queryArriveStations(TicketQueryParam queryParam, String appVer) {
        ArriveStationsResultBean arriveStationsResult = new ArriveStationsResultBean();// HPE到达站列表返回结果

        ArriveStationsParamBean arriveStationsParam = new ArriveStationsParamBean();
        arriveStationsParam.setStartCityId(queryParam.getStartCityId());
        arriveStationsParam.setStartStationId(queryParam.getStartStationId());
        arriveStationsParam.setArriveStationName(queryParam.getArriveStationName());
        // 当代集团的车站只查询省内到达站
        if (WebAppVersion.NC.equals(appVer) || WebAppVersion.YLNS.equals(appVer)) {
            arriveStationsParam.setArriveStationType(TrafficlineNC.SX);
        }
        try {
            arriveStationsResult = stationService.getArriveStations(arriveStationsParam);
        } catch (ServiceException e) {
            LOGGER.error("调用到达站列表服务发生异常", e);
        }

        return arriveStationsResult;
    }
}
